# Definition for a binary tree node.
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class Solution:
    def pathSum(self, root: TreeNode, sum: int) -> list:
        if not root:
            return []
        self.res, self.sum = [], sum
        self.search(0, root, [])
        return self.res

    def search(self, now_sum, note: TreeNode, list_in: list):
        mid_num = now_sum + note.val
        list_in.append(note.val)
        if mid_num == self.sum and not note.left and not note.right:
            self.res.append(list_in)
        else:
            if note.left:
                self.search(mid_num, note.left, list_in[:])
            if note.right:
                self.search(mid_num, note.right, list_in[:])
